import os.path

from gxl_ai_utils.utils import utils_file


def do_handle():
    """"""
    input_root_dir = "/home/work_nfs8/asr_data/data/asr_test_sets"
    testset_names,_ = utils_file.do_listdir(input_root_dir)
    for testset_path in testset_names:
        print(f'handle {testset_path}')
        wav_dir = os.path.join(testset_path, 'wav')
        if not os.path.exists(wav_dir):
            print(f'{wav_dir} 不是一个测试集')
            continue
        wav_dict_path = os.path.join(testset_path, 'wav.scp')
        if not os.path.exists(wav_dict_path):
            print(f'{wav_dict_path} not 存在')
            continue
        wav_dict = utils_file.load_dict_from_scp(wav_dict_path)
        new_wav_dict = {}
        for key,wav_path in wav_dict.items():
            """"""
            file_name = os.path.basename(wav_path)
            new_wav_path = os.path.join(wav_dir, file_name)
            new_wav_dict[key] = new_wav_path
        utils_file.write_dict_to_scp(new_wav_dict, wav_dict_path)
        text_dict_path = os.path.join(testset_path, 'text')
        data_list_path = os.path.join(testset_path, 'data.list')
        dict_list = utils_file.load_dict_list_from_jsonl(data_list_path)
        new_dict_list = []
        for dict_item in dict_list:
            """"""
            old_path = dict_item["wav"]
            file_name = os.path.basename(old_path)
            new_wav_path = os.path.join(wav_dir, file_name)
            dict_item['wav'] = new_wav_path
            new_dict_list.append(dict_item)
        utils_file.write_dict_list_to_jsonl(new_dict_list, data_list_path)




if __name__=="__main__":
    """"""
    do_handle()